(function(window, jQuery) {
    /*
    -------------------------------------------------
    @projec_t   资车贷分页插件
    @au_thor    chenyang
    @crea_te    2015.11.18
    -------------------------------------------------
    */
    $.fn.extend({
        zcdFY: function(options) {
            var _this = this;
            // 默认配置项
            var settings = {
                url: "",
                //http请求地址
                _arr: [],
                //空数组
                _cur: 6,
                //当前页
                _max: 10,
                //最大页
                _tempMax: 0,
                //临时页数
                _num: 5,
                //单次显示个数
                _tempNum: 0,
                //临时显示个数
                _t: 0,
                _param: ""
            };
            //合并配置项
            settings = $.extend(true, {}, settings, options || {});
            settings._tempMax = settings._max;
            settings._tempNum = settings._num;
            /*
            ------------------------------------------------------
            Ajax 

            @url  : 请求地址
            @page : 页码
            ------------------------------------------------------
            */
            function Ajax(url, cpage) {
                $.get(url, settings._param + "&curpage=" + cpage, function(data) {
                    $("p").text(data);
                });
            }
            /*
            ------------------------------------------------------
            更新页码class 

            @idx  : 待更新的页码值
            ------------------------------------------------------
            */
            function updateClass(idx) {
                $(".pag").removeClass("current").each(function(k, v) {
                    if (parseInt($(v).text()) === idx) {
                        $(v).addClass("current");
                        return false;
                    }
                });
            }

            function init() {
                var html = [];
                var str = "<span class='pags'>";
                html.push('<span><a href="javascript:;" class="topPage disabled">首页</a><a href="javascript:;" class="prev disabled">上一页</a></span>');
                for (var i = 1; i <= settings._num; i++) {
                    if (i === 1) {
                        str += '<a href="javascript:;" class="pag current">' + i + "</a>";
                    } else {
                        str += '<a href="javascript:;" class="pag">' + i + "</a>";
                    }
                }
                str += "</span>";
                html.push(str);
                html.push('<span><a href="javascript:;" class="next">下一页</a><a href="javascript:;" class="endPage">尾页</a></span><span class="_tags">共' + settings._max + "页</span>");
                $(_this).html(html.join(""));
                Ajax(settings.url, 1);
            }
            /*
            ------------------------------------------------------
            首页 
            ------------------------------------------------------
            */
            $("body").on("click", ".topPage", function() {
                settings._arr.length = 0;
                $(".prev").add($(".topPage")).addClass("disabled");
                $(".next").add($(".endPage")).removeClass("disabled");
                while (settings._num) {
                    settings._arr.unshift('<a href="javascript:;" class="pag">' + settings._num-- + "</a>");
                }
                settings._num = settings._tempNum;
                $(".pags").html(settings._arr.join(""));
                settings._cur = 1;
                updateClass(settings._cur);
                Ajax(settings.url, settings._cur);
            });
            /*
            ------------------------------------------------------
            尾页 
            ------------------------------------------------------
            */
            $("body").on("click", ".endPage", function() {
                settings._arr.length = 0;
                $(".prev").add($(".topPage")).removeClass("disabled");
                $(".next").add($(".endPage")).addClass("disabled");
                settings._num = settings._max % settings._num ? settings._max % settings._num : settings._num;

                while (settings._num) {
                    settings._arr.unshift('<a href="javascript:;" class="pag">' + settings._max-- + "</a>");
                    settings._num--;
                }
                settings._num = settings._tempNum;
                settings._max = settings._tempMax;
                $(".pags").html(settings._arr.join(""));
                settings._cur = settings._max;
                updateClass(settings._cur);
                Ajax(settings.url, settings._cur);
            });
            /*
            ------------------------------------------------------
            上一页
            ------------------------------------------------------
            */
            $("body").on("click", ".prev", function() {
                var iN = parseInt($(".pag:first").text());
                if (settings._cur > 1) {
                    // settings._cur++;
                    settings._arr.length = 0;
                    $(".next").add($(".endPage")).removeClass("disabled");
                    if (settings._cur === iN) {
                        var sCur = "pag";
                        while (settings._num) {
                            settings._arr.unshift('<a href="javascript:;" class="pag">' + --iN + "</a>");
                            settings._num--;
                        }
                        settings._num = settings._tempNum;
                        $(".pags").html(settings._arr.join(""));
                    }
                    settings._cur--;
                    updateClass(settings._cur);
                    Ajax(settings.url, settings._cur);
                }
                if (settings._cur === 1) {
                    $(this).add($(".topPage")).addClass("disabled");
                }
            });
            /*
            ------------------------------------------------------
            下一页
            ------------------------------------------------------
            */
            $("body").on("click", ".next", function() {
                var iN = parseInt($(".pag:last").text());
                if (settings._cur < settings._max) {
                    settings._arr.length = 0;
                    $(".prev").add($(".topPage")).removeClass("disabled");
                    if (settings._cur === iN) {
                        var sCur = "pag";
                        settings._num = settings._max - settings._cur > settings._num ? settings._num : settings._max - settings._cur;
                        while (settings._num) {
                            settings._arr.push('<a href="javascript:;" class="pag">' + ++iN + "</a>");
                            settings._num--;
                        }
                        settings._num = settings._tempNum;
                        $(".pags").html(settings._arr.join(""));
                    }
                    settings._cur++;
                    updateClass(settings._cur);
                    Ajax(settings.url, settings._cur);
                }
                if (settings._cur === settings._max) {
                    $(this).add($(".endPage")).addClass("disabled");
                }
            });
            /*
            ------------------------------------------------------
            页码请求
            ------------------------------------------------------
            */
            $("body").on("click", ".pag", function() {
                settings._cur = parseInt($(this).text());
                updateClass(settings._cur);
                Ajax(settings.url, settings._cur);
                if (1 < settings._cur && settings._cur < settings._max) {
                    $(".prev").add($(".topPage")).removeClass("disabled");
                    $(".next").add($(".endPage")).removeClass("disabled");
                } else if (settings._cur === 1) {
                    $(".prev").add($(".topPage")).addClass("disabled");
                } else if (settings._cur === settings._max) {
                    $(".next").add($(".endPage")).addClass("disabled");
                }
            });
            init();
        }
    });
})(window, jQuery);
